<?php
namespace caroltc\jenkinsapp\library;
/**
 * Created by PhpStorm.
 * User: root
 * Date: 11/13/18
 * Time: 3:14 PM
 */
class LibPDO
{
    private $pdo=null;
    private $dsn = '';

    // We'll use a constructor, as you can't directly call a function
    // from a property definition.
    public function __construct($params)
    {
        try {
            $host=$params['host'];
            $port=$params['port'];
            $username=$params['username'];
            $password=$params['password'];
            $database=$params['database'];
            $this->pdo = new \PDO('mysql:host='.$host.';port='.$port.';dbname='.$database.';charset=utf8',$username,$password,
                array(\PDO::ATTR_EMULATE_PREPARES => false)
            );
            $this->dsn = 'mysql:host='.$host.';port='.$port.';dbname='.$database.';charset=utf8';
            $this->pdo->query("set names utf8");
            // var_dump($this->pdo->query("SELECT 1")->fetchAll(PDO::FETCH_ASSOC));
        } catch (\PDOException $e) {
             throw new \Exception('db connect failed. ' . $e->getMessage(), 1);
        }
    }

    public function getAll($sql){
        $stmt=$this->pdo->query($sql);
        if (false === $stmt) {
            throw new \Exception('[SQL_ERROR][DSN:'.$this->dsn.']' . $sql);
        }
        $rows=$stmt->fetchAll(\PDO::FETCH_ASSOC);
        return $rows;
    }

    public function getCol($sql){
        $stmt=$this->pdo->query($sql);
        $rows=$stmt->fetchAll(\PDO::FETCH_BOTH);
        $col=array();
        if($rows){
            foreach ($rows as $row) {
                $col[]=$row[0];
            }
        }
        return $col;
    }

    public function getRow($sql){
        $stmt=$this->pdo->query($sql);
        $rows=$stmt->fetchAll(\PDO::FETCH_ASSOC);
        if($rows)
            return $rows[0];
        else return false;
    }

    public function getOne($sql){
        //FETCH_BOTH
        $stmt=$this->pdo->query($sql);
        $rows=$stmt->fetchAll(\PDO::FETCH_BOTH);
        if($rows){
            $row = $rows[0];
            if($row){
                return $row[0];
            }else{
                return false;
            }
        }
        else return false;
    }

    public function exec($sql){
        $rows=$this->pdo->exec($sql);
        return $rows;
    }

    public function save($table, $data)
    {
        $keys = implode(',', array_keys($data));
        $values = implode("','", array_values($data));
        $sql = "INSERT INTO {$table} ($keys) VALUES ('{$values}')";
        return $this->insert($sql);
    }

    public function update($table, $where_sql, $data)
    {
        $update_sql_arr =[];
        foreach ($data as $key => $value) {
            $update_sql_arr[] = "{$key} = '$value'";
        }
        $update_sql = implode(",", $update_sql_arr);

        $sql = "UPDATE {$table} SET $update_sql WHERE {$where_sql}";
        return $this->pdo->exec($sql);
    }

    public function insert($sql){
        $rows=$this->pdo->exec($sql);
        if($rows){
            return $this->pdo->lastInsertId();
        }else{
            return false;
        }
    }

    public function beginTransaction(){
        return $this->pdo->beginTransaction();
    }
    public function commit(){
        return $this->pdo->commit();
    }
    public function rollBack(){
        return $this->pdo->rollBack();
    }
    public function inTransaction(){
        return $this->pdo->inTransaction();
    }

    public function errorCode(){
        return $this->pdo->errorCode();
    }
    public function errorInfo(){
        return $this->pdo->errorInfo();
    }

    public function quote($string,$parameter_type = \PDO::PARAM_STR ){
        return $this->pdo->quote($string,$parameter_type);
    }
}